#include "fig_transform_enemy.h"
#include "fig_enemybullet_master.h"
#include "fighter.h"
#include "fig_sounds.h"
#include "fig_enemy_craft.h"

#include "num/rand.h"
#include "math/facing.h"
#include "dd/dd_man.h"
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging
#include "util/timer.h"		    // logging

FIGTransformEnemy::FIGTransformEnemy(FIGEnemyCraft::ENEMY_TYPES EnemyType) : 
									FIGEnemyCraft(EnemyType),
									m_ShootStyleIndex(0),
									m_bShouldTakeLessDamage(false)
{
	m_Size = m_Bone.Load(FIGEnemyCraft::MapTypeToBoneFile(m_EnemyType));
	m_Radius = m_Size.Magnitude() * 0.35f;
	m_IsHitTestCircle = false;

	m_OffsetScale = 0.5f;
}



void	FIGTransformEnemy::Tick(float dt)
{
	
	FIGEnemyCraft::Tick(dt);

	if(m_DelaySpawn > 0)
		return;

	if(!m_IsAlive && !m_Bone.IsExploding())
		return;

	m_Bone.SetPos(m_Pos);
	m_Bone.SetRotation(m_Rotation);
	m_Bone.Tick(dt);
	
}


void		FIGTransformEnemy::Render(const math::Vec2& Offset,float Scale)
{
	if(m_IsAlive || m_Bone.IsExploding() || m_DelayDie > 0)
	{
		m_Bone.Render(Offset,Scale);
	}

	if(m_IsAlive)
		FIGEnemyCraft::Render(Offset,Scale);

	
}


void		FIGTransformEnemy::RenderShadow(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;

	m_Bone.RenderShadow(Offset,Scale);
	FIGEnemyCraft::RenderShadow(Offset,Scale);
}

void		FIGTransformEnemy::ReSpawn()
{
	
	m_Bone.Reset();
	FIGEnemyCraft::ReSpawn();
	m_bShouldTakeLessDamage = true;
}


bool		FIGTransformEnemy::HitTest(FIGBullet* pBullet)
{
	if(!m_IsAlive)
		return false;

	if(m_Life <= 0)
		return false;

	if(m_IsHitTestCircle)
	{
		if(FIGCraft::HitTest(pBullet))
			return true;
	}
	else
	{
		bool b =m_Bone.HitTest(pBullet->GetPos());
		if(b)
		{
			GotDamage(pBullet->GetDamagePower(),pBullet,false);
			pBullet->Kill();
			return true;
		}
	}
	
	return false;
}

bool	FIGTransformEnemy::HitTest(const math::Vec2& Point)
{
	if(!m_IsAlive)
		return false;

	if(m_Life <= 0)
		return false;

	bool b =m_Bone.HitTest(Point);
	return b;
}

bool	FIGTransformEnemy::HitTest(FIGLifeObj* pObj)
{
	//return false;

	if(!m_IsAlive)
		return false;

	if(m_Life <= 0)
		return false;

	const float Radius = pObj->GetRadius() * Fighter::Instance().GetScale();
	
	if(m_Bone.HitTest( pObj->GetPos() - math::Vec2(0,-Radius)))
		return true;

	if(m_Bone.HitTest( pObj->GetPos() - math::Vec2(0, Radius)))
		return true;

	if(m_Bone.HitTest( pObj->GetPos() - math::Vec2( -Radius,0)))
		return true;

	if(m_Bone.HitTest( pObj->GetPos() - math::Vec2(Radius,0)))
		return true;

	if(m_Bone.HitTest( pObj->GetPos()))
		return true;
	 
	
	return false;
}

void	FIGTransformEnemy::OnExplode()
{
	FIGEnemyCraft::OnExplode();
	m_Bone.Explode(this);
}

bool	FIGTransformEnemy::IsReadyToSpawn()
{
	return (IsAlive() == false && m_Bone.IsExploding() == false);
}

void	FIGTransformEnemy::OnShoot()
{
	switch(m_ShootStyleIndex)
	{
	case 0:
		OnShootStyle1();
		break;
	case 1:
		OnShootStyle2();
		break;
	case 2:
		OnShootStyle3();
		break;
	case 3:
		OnShootStyle4();
		break;
	}
	m_ShootStyleIndex++;
	if(m_ShootStyleIndex == 4)
		m_ShootStyleIndex = 0;

	m_bShouldTakeLessDamage = false;
}

void	FIGTransformEnemy::OnShootStyle1()
{   	

}

void	FIGTransformEnemy::OnShootStyle2()
{
	m_ShootStyleIndex = 0;
	OnShootStyle1();
}

void	FIGTransformEnemy::OnShootStyle3()
{
	m_ShootStyleIndex = 0;
	OnShootStyle1();
}

void	FIGTransformEnemy::OnShootStyle4()
{
	m_ShootStyleIndex = 0;
	OnShootStyle1();
}


